import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { getArtistSublistAPI, getDigitalAlbumPurchasedAPI, getMvSublistAPI, getPlaylistDetailAPI, getUserRecordAPI, getUserCloudAPI } from "../api";
import { getSongDownloadAPI } from "@/views/player/api";
import { RootState } from "@/store";
import storage from "@/utils/Storage";
import { userSubcountAPI } from "@/views/account/api";
import { getPlaylistTrackAllAPI } from "@/views/playlist/api";

interface IPlaylistArg {
  id: number,
  limit?: number,
  offset?: number,
  currpage?: number
}

export const feachPlaylistDetailAction = createAsyncThunk<void, IPlaylistArg, { state: RootState }>("playlistDetails", async (arg, { dispatch, getState }) => {
  const { id, limit = 12, offset = 0, currpage = 1 } = arg
  if (storage.get("data").modeType === "queryType") {
    const { playlist } = await userSubcountAPI(id)
    dispatch(changeAllPlaylistAction(playlist))

    const { songs } = await getPlaylistTrackAllAPI(playlist[0].id, limit, offset)
    if (limit > 12) {
      dispatch(changeTracksAction(songs))
    } else {
      dispatch(clearTracksAction(songs))
    }

    const { paidAlbums } = await getDigitalAlbumPurchasedAPI()
    dispatch(changeAlbumsAction(paidAlbums))

    const { data } = await getArtistSublistAPI()
    dispatch(changeArtistListAction(data))

    const res = await getMvSublistAPI()
    dispatch(changeMvListAction(res.data))

    const value = await getUserCloudAPI(200, 0)
    dispatch(changeCloundAction(value.data))

  } else {
    const { playlist } = await getPlaylistDetailAPI(id)
    const trackIds = playlist.trackIds.map((item: any) => item.id)
    let newTrackIds = getState().musicLibrary.tracks

    if (limit > 12) {
      let newoffset = newTrackIds.length === 12 ? offset + 12 : offset
      const { songs } = await getSongDownloadAPI(trackIds.slice(newoffset, currpage * limit).join(","))
      dispatch(changeTracksAction(songs))
    } else {
      dispatch(clearTracksAction([]))
      dispatch(changeTracksAction(playlist.tracks.slice(0, limit)))
    }
  }

  // if (newTrackIds.length > 0) {
  //   for (const item of trackIds) {
  //     newTrackIds = newTrackIds.filter((track: any) => track.id !== item)
  //   }
  // } else {
  //   newTrackIds = trackIds
  // }
})

interface IPlayhistory {
  id: number,
  type?: number
}

export const feachUserPlayHistoryAction = createAsyncThunk("playhistory", async (arg: IPlayhistory, { dispatch }) => {
  const { id, type = 1 } = arg
  if (type === 1) {
    const { weekData } = await getUserRecordAPI(id, type)
    dispatch(changePlayHistoryAction(weekData))
  } else {
    const { allData } = await getUserRecordAPI(id, type)
    dispatch(changePlayHistoryAction(allData))
  }
})

interface IState {
  tracks: any[],
  allTracks: any[],
  allPlaylist: any[],
  paidAlbums: any[],
  artistList: any[],
  mvList: any[],
  userCloudList: any[],
  playHistorys: any[],
}
const initialState: IState = {
  tracks: [],
  allTracks: [],
  allPlaylist: [],
  paidAlbums: [],
  artistList: [],
  mvList: [],
  userCloudList: [],
  playHistorys: []
}
// 
const musiclibrarySlice = createSlice({
  name: "musiclibrary",
  initialState,
  reducers: {
    changeTracksAction(state, { payload }) {
      state.allTracks = [...state.allTracks, ...payload]
    },
    clearTracksAction(state, { payload }) {
      state.tracks = payload
    },
    changeAllPlaylistAction(state, { payload }) {
      state.allPlaylist = payload
    },
    changeAlbumsAction(state, { payload }) {
      state.paidAlbums = payload
    },
    changeArtistListAction(state, { payload }) {
      state.artistList = payload
    },
    changeMvListAction(state, { payload }) {
      state.mvList = payload
    },
    changeCloundAction(state, { payload }) {
      state.userCloudList = payload
    },
    changePlayHistoryAction(state, { payload }) {
      state.playHistorys = payload
    }
  }
})

export const { changeTracksAction, clearTracksAction,
  changeAllPlaylistAction, changeAlbumsAction,
  changeArtistListAction, changeMvListAction,
  changeCloundAction, changePlayHistoryAction } = musiclibrarySlice.actions
export default musiclibrarySlice.reducer